package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import negocio.Alumno;
import negocio.AlumnoControlador;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AlumnosEditar extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JLabel lblProvincia;
	private JPanel panel;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private AlumnosBuscar padre;
	private Alumno alumno;

	public void llenarDatos()
	{
		textField.setText(alumno.getDni());
		textField.setEditable(false);
		textField.setEnabled(false);
		textField_1.setText(alumno.getNombre());
		textField_1.setEditable(false);
		textField_1.setEnabled(false);
		textField_2.setText(alumno.getApellido());
		textField_2.setEditable(false);
		textField_2.setEnabled(false);
		textField_3.setText(alumno.getTelefono());
		textField_4.setText(alumno.getMail());
		textField_5.setText(alumno.getDireccion());		
		textField_6.setText(alumno.getPartido());
		textField_7.setText(alumno.getProvincia());		
	}

	
	public AlumnosEditar(AlumnosBuscar padre, Alumno alumno) {
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				padre.nullAlumnosEditar();
			}
		});
		this.padre=padre;
		this.alumno=alumno;
		setTitle("Detalles del alumno");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 553, 241);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Datos de Alumno", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(16, 19, 504, 136);
		contentPane.add(panel);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(60, 16, 183, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(60, 47, 183, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(60, 78, 183, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(315, 16, 183, 20);
		panel.add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(315, 47, 183, 20);
		panel.add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(315, 78, 183, 20);
		panel.add(textField_5);
		textField_5.setColumns(10);
		
		textField_6 = new JTextField();
		textField_6.setBounds(60, 109, 183, 20);
		panel.add(textField_6);
		textField_6.setColumns(10);
		
		textField_7 = new JTextField();
		textField_7.setBounds(315, 109, 183, 20);
		panel.add(textField_7);
		textField_7.setColumns(10);
		
		textField.setText("");
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
		textField_5.setText("");
		textField_6.setText("");
		textField_7.setText("");
		
		JLabel lblDni = new JLabel("DNI: ");
		lblDni.setBounds(6, 19, 34, 14);
		panel.add(lblDni);
		
		JLabel lblNombre = new JLabel("Nombre: ");
		lblNombre.setBounds(6, 50, 46, 14);
		panel.add(lblNombre);
		
		JLabel lblApellido = new JLabel("Apellido: ");
		lblApellido.setBounds(6, 81, 46, 14);
		panel.add(lblApellido);
		
		JLabel lblTelefono = new JLabel("Telefono: ");
		lblTelefono.setBounds(253, 19, 52, 14);
		panel.add(lblTelefono);
		
		JLabel lblMail = new JLabel("Mail: ");
		lblMail.setBounds(253, 50, 34, 14);
		panel.add(lblMail);
		
		JLabel lblDireccion = new JLabel("Direccion: ");
		lblDireccion.setBounds(253, 81, 52, 14);
		panel.add(lblDireccion);
		
		JLabel lblPartido = new JLabel("Partido: ");
		lblPartido.setBounds(6, 112, 46, 14);
		panel.add(lblPartido);
		
		lblProvincia = new JLabel("Provincia: ");
		lblProvincia.setBounds(253, 112, 52, 14);
		panel.add(lblProvincia);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				padre.nullAlumnosEditar();
				dispose();
			}
		});
		btnCancelar.setBounds(431, 166, 89, 23);
		contentPane.add(btnCancelar);
		
		btnAceptar = new JButton("Guardar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				AlumnoControlador.actualizar(alumno, textField_3.getText(),textField_4.getText(),textField_5.getText(),textField_6.getText(), textField_7.getText() );
				padre.nullAlumnosEditar();
				padre.llenarTabla();
				dispose();
				
				
				
			}
		});
		btnAceptar.setBounds(332, 166, 89, 23);
		contentPane.add(btnAceptar);
		this.llenarDatos();
	}
}
